package practise.mulitThreads.EventSystem.impl;

import practise.mulitThreads.EventSystem.Client;
import practise.mulitThreads.EventSystem.EventContainer;
import practise.mulitThreads.EventSystem.entity.Event;

import java.util.concurrent.TimeUnit;

/**
 * Created by angela on 7/27/2014.
 */
public class ClientImpl implements Client {
    private static int clientNumber =0;
    EventContainer eventContainer;
    private int id;

    public ClientImpl(){
        clientNumber++;
        id=clientNumber;
    }

    @Override
    public void consume(Event event) {
        System.out.println("Client:"+id+" Consume a event id:"+event.getId()+ " content :"+event.getMessage());

    }

    @Override
    public void register(EventContainer container) {
        this.eventContainer = container;
    }

    @Override
    public void run() {
        try {
            while (!Thread.currentThread().isInterrupted()) {

                Thread.sleep(1000);

                consume(eventContainer.poll());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
